package study.chapter25.tree;

import java.util.Stack;

/**
 * RecursiveBinaryTree
 *
 * @author byc
 * @date 2021-11-28
 */
public class RecursiveBinaryTree {

    private static void recursive(Node node){
        if (node == null){
            return;
        }
        recursive(node.left);
        recursive(node.right);
    }

    //先序遍历
    private static void preOrder(Node node){
        if (node == null){
            return;
        }
        System.out.println(node.data);
        preOrder(node.left);
        preOrder(node.right);
    }

    //中序遍历
    private static void inOrder(Node node){
        if (node == null){
            return;
        }
        inOrder(node.left);
        System.out.println(node.data);
        inOrder(node.right);
    }

    //后序遍历
    private static void postOrder(Node node){
        if (node == null){
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.data);
    }

    public static void main(String[] args) {
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);

        preOrder(root);

    }

    //栈 遍历树节点
    private static void pre(Node node){
        Stack<Node> stack = new Stack<>();
        stack.push(node);

        while (!stack.isEmpty()){
            node = stack.pop();

            System.out.println(node.data);
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }

        }
    }
}
